﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class TradeDetailController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /TradeDetail/

        public ActionResult Index(int uid, string pdate)
        {
            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
            th = th.GetSettlementModel(pdate, db);//查询数据库
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            cookie.SetCookie("querydate", pdate, 20);
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasOperatorPrower(operId, uid, db))
            {
                if (th != null && th.YPdate != 0)
                {
                    int tid = th.Id;
                    int operatorId = uid;
                    BenefitHelper.Sys.Operator op = new BenefitHelper.Sys.Operator().GetModel(operatorId, db);
                    ViewBag.op = op;


                    ViewBag.uid = uid;
                    ViewBag.pdate = pdate;
                    string stringPdate = new BenefitHelper.Sys.TradeHistory().AddHorizontalLineIntoPdate(pdate);
                    ViewBag.Hpdate = stringPdate;
                    bool traded = op.OperatorTradedAtDay(operatorId, tid, db);
                    if (traded)
                    {

                        List<BenefitHelper.Sys.Account> accounts = new BenefitHelper.Sys.DayOperatorAccount().GetDayOperatorAccounts(th.GetDateTimeFromTradeHistoryId(th.Id, db), operatorId, db);
                        List<BenefitHelper.Data.Day.AccountDayMoney> accountMoneys = new List<BenefitHelper.Data.Day.AccountDayMoney>();
                        foreach (BenefitHelper.Sys.Account account in accounts)
                        {
                            BenefitHelper.Data.Day.AccountDayMoney m = new BenefitHelper.Data.Day.AccountDayMoney();
                            m = m.GetAccountDayMoney(th.Id, account.Id, db);
                            accountMoneys.Add(m);
                        }
                        ViewBag.accountMoneys = accountMoneys;

                        BenefitHelper.View.OperatorDayMoney operatorMoney = new BenefitHelper.View.OperatorDayMoney().GetOperatorDayMoney(accountMoneys, operatorId, tid);
                        if (operatorMoney != null)
                        {
                            ViewBag.operatorMoney = operatorMoney;
                        }
                        else
                        {
                            ViewBag.operatorMoney = new BenefitHelper.View.OperatorDayMoney();
                        }




                        List<BenefitHelper.View.OperatorDayTick> winTicks = new BenefitHelper.View.OperatorDayTick().GetOperatorDayWinOrLostTicks(operatorId, th.Id, true, db);
                        List<BenefitHelper.View.OperatorDayTick> lostTicks = new BenefitHelper.View.OperatorDayTick().GetOperatorDayWinOrLostTicks(operatorId, th.Id, false, db);
                        string js = "";
                        js += BenefitHelper.Tools.ChartAtManager.TickPie(winTicks, "showflash4", "盈利Tick分布", "TickPie");
                        js += BenefitHelper.Tools.ChartAtManager.TickPie(lostTicks, "showflash2", "亏损Tick分布", "TickPie2");
                        ViewBag.js = js;

                        BenefitHelper.View.OperatorDayProductInfo info = new BenefitHelper.View.OperatorDayProductInfo();
                        List<BenefitHelper.View.OperatorDayProductInfo> infos = info.GetSortedOperatorDayProductInfo(th.Id, operatorId, db);
                        ViewBag.infos = infos;

                        List<BenefitHelper.View.OperatorDayPosition> positions = new BenefitHelper.View.OperatorDayPosition().GetAllAccountDayPositionDelivery(th.Id, operatorId, db);

                        ViewBag.allPosition = positions;
                        db.Dispose();


                        return View();
                    }
                    else
                    {
                        db.Dispose();
                        return Redirect("/Error/index/" + uid + "/" + pdate);
                    }
                }
                else
                {
                    db.Dispose();
                    return Redirect("/Error/index/" + uid + "/" + pdate);
                }
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
        }
        public ActionResult ShowDetails()
        {
            return View();
        }
    }
}
